package com.brook;

import com.brook.demo.pojo.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试包含网关。inclusive gateway
 */
public class ActivitiGatewayInclusive {

    @Test
    public void testDeployment(){
        //get process engine
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //get deployService
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //start deploy
        Deployment deploy = repositoryService.createDeployment()
                .name("出差申请-包含网关")
                .addClasspathResource("bpmn/evection-inclusive.bpmn")
                .deploy();
        //输出
        System.out.println("流程部署id："+deploy.getId());
        System.out.println("流程部署name："+deploy.getName());
    }

    @Test
    public void testStartProcess() {
        //get process engine
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //get start Service
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //process definition id
        String key = "evectionInclusive";
        //set condition variables
        Map<String, Object> map = new HashMap<>();
        Evection evection = new Evection();
        evection.setNum(4d);
        map.put("evection", evection);

        //latch
        runtimeService.startProcessInstanceByKey(key, map);
    }

    @Test
    public void completeTask() {
        //get process engine
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //get task service
        TaskService taskService = defaultProcessEngine.getTaskService();
        //process definition id
        String key = "evectionInclusive";
        //processInstanceId
        //String instanceId = "140001";
        //definition process task assignee
        //String assignee = "Brook";
        //String assignee = "Ada";
        String assignee = "Eric";

        //retrieve process task
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee(assignee)
                .singleResult();
        System.out.println("----------------------------"+task+"--------------------------------");
        //start complete task
        if (task != null) {
            taskService.complete(task.getId());
            System.out.println("process task"+task.getName()+assignee+"approve done.please continue!");
        }

    }}
